<?php
class Mageex_RelatedProduct_Block_Adminhtml_RelatedProduct_Edit_Tab_Related
    extends Mage_Adminhtml_Block_Widget_Form
    implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
    /**
     * Prepare content for tab
     *
     * @return string
     */
    public function getTabLabel()
    {
        return Mage::helper('catalogrule')->__('Conditions');
    }

    /**
     * Prepare title for tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return Mage::helper('catalogrule')->__('Conditions');
    }

    /**
     * Returns status flag about this tab can be showen or not
     *
     * @return true
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * Returns status flag about this tab hidden or not
     *
     * @return true
     */
    public function isHidden()
    {
        return false;
    }

    protected function _prepareForm()
    {
        $model = Mage::getModel('catalogrule/rule');
		$relatedproduct = Mage::registry('relatedproduct_data');
		$model->getConditions()->setJsFormObject('rule_conditions_fieldset');

        //$form = new Varien_Data_Form(array('id' => 'edit_form1', 'action' => $this->getData('action'), 'method' => 'post'));
        $form = new Varien_Data_Form();
		$this->setForm($form);
		
        $form->setHtmlIdPrefix('rule_');
		/*
        $rendererGeneral = Mage::getBlockSingleton('adminhtml/widget_form_renderer_fieldset')
            ->setTemplate('relatedproduct/fieldset.phtml');
		
		$general = $form->addFieldset('general_fieldset', array(
            'legend'=>Mage::helper('catalogrule')->__('General'))
        )->setRenderer($rendererGeneral);
		*/
		
		$select = $form->addFieldset('relatedproduct_select', array('legend'=>Mage::helper('relatedproduct')->__('Select Product')));
        $select->addField('number_product', 'text', array(
			  'label'     => Mage::helper('relatedproduct')->__('Number of products'),
			  'class'     => 'required-entry',
			  'required'  => true,
			  'name'      => 'number_product',
			  'value'	  => $relatedproduct->getNumberProduct(),
		  ));
		$select->addField('random', 'select', array(
          'label'     => Mage::helper('relatedproduct')->__('Random Order'),
          'name'      => 'random',
		  'required'  => true,
		  'value'	  => $relatedproduct->getRandom(),
          'values'    => Mage::getModel('relatedproduct/random')->toOptionArray()
      ));
		$renderer = Mage::getBlockSingleton('adminhtml/widget_form_renderer_fieldset')
            ->setTemplate('relatedproduct/product/fieldset.phtml')
            ->setNewChildUrl($this->getUrl('adminhtml/promo_catalog/newConditionHtml/form/rule_conditions_fieldset'));
		
        $fieldset = $form->addFieldset('conditions_product_fieldset', array(
            'legend'=>Mage::helper('catalogrule')->__('Conditions (leave blank for all products)'))
        )->setRenderer($renderer);

        $fieldset->addField('conditions_related', 'text', array(
            'name' => 'conditions_related',
            'label' => Mage::helper('catalogrule')->__('Conditions'),
            'title' => Mage::helper('catalogrule')->__('Conditions'),
            'required' => true,
        ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/conditions'));
		
		
		
		if ( Mage::getSingleton('adminhtml/session')->getRelatedProductData() )
		  {
			  $form->setValues(Mage::getSingleton('adminhtml/session')->getRelatedProductData());
			  Mage::getSingleton('adminhtml/session')->setRelatedProductData(null);
		  } elseif ( Mage::registry('relatedproduct_data') ) {
			  $form->setValues(Mage::registry('relatedproduct_data')->getData());
		  }

        //$form->setUseContainer(true);

        

        return parent::_prepareForm();
    }
}
